package com.ddl.service.impl;

import com.ddl.common.exception.ExceptionUtil;
import com.ddl.common.exception.ResultModel;
import com.ddl.dao.ADMapper;
import com.ddl.enums.ADTypeENum;
import com.ddl.pojo.AD;
import com.ddl.service.ADService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2018-12-11
 */
@Service
public class ADServiceImpl implements ADService {

    private final static Logger logger = LoggerFactory.getLogger(ADServiceImpl.class);

    @Autowired
    ADMapper adMapper;

    @Override
    public ResultModel getAD() {
        try {
            List<AD> adList = adMapper.getAll();
            if (CollectionUtils.isEmpty(adList)) {
                return ResultModel.getSuccessInstance();
            }
            Map<String, Object> map = new HashMap<>();
            List<AD> recommendList = new ArrayList<>();
            List<AD> scenesList = new ArrayList<>();
            List<AD> showList = new ArrayList<>();

            for (AD ad : adList) {
                if (ADTypeENum.recommend.type.equalsIgnoreCase(ad.getType())) {
                    recommendList.add(ad);
                } else if (ADTypeENum.scenes.type.equalsIgnoreCase(ad.getType())) {
                    scenesList.add(ad);
                } else if (ADTypeENum.scenes.type.equalsIgnoreCase(ad.getType())) {
                    showList.add(ad);
                }
            }
            map.put("recommendList", recommendList);
            map.put("scenesList", scenesList);
            map.put("showList", showList);
            ResultModel resultModel = ResultModel.getSuccessInstance();
            resultModel.setData(map);
            return resultModel;
        } catch (Exception e) {
            logger.error("getAD fail, ", e);
            return ExceptionUtil.getMessageException(e);
        }
    }

    @Override
    public ResultModel getADDetail(int adId) {
        AD ad = adMapper.selectByPrimaryKey(adId);
        ResultModel resultModel = ResultModel.getSuccessInstance();
        resultModel.setData(ad);
        return resultModel;
    }

}
